package com.starbucks.opencase.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.starbucks.opencase.enums.enumCMKey;
import com.starbucks.opencase.enums.enumCMMark;
import com.starbucks.opencase.enums.enumCMTip;
import com.starbucks.opencase.function.funCarry;
import com.starbucks.opencase.function.funOnHook;
import com.starbucks.opencase.function.funSession;
import com.starbucks.opencase.function.funUserTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class serUserOnHook {
    @Autowired
    private funSession funSession;
    @Autowired
    private funUserTest funUserTest;
    @Autowired
    private funCarry funCarry;
    @Autowired
    private funOnHook funOnHook;
    public void addOnHook(String id, JsonNode message){
        ObjectMapper om=new ObjectMapper();
        ObjectNode on=om.createObjectNode();
        on.put(enumCMKey.mark.name(), enumCMMark.addOnHook.ordinal());
        on.put(enumCMKey.timestamp.name(), System.currentTimeMillis());
        on.put(enumCMKey.status.name(), false);
        if(!funSession.isOnlineOfId(id,on))return;
        if(!funUserTest.hasPID(id,message,on))return;
        String uid=funSession.getUIdOfId(id);
        String pid=message.get(enumCMKey.pid.name()).asText();
        if(funCarry.isCarry(uid,pid) || funOnHook.isExit(uid,pid)){
            on.put(enumCMKey.tip.name(), enumCMTip.not_pet_leisure.getTip());
            funSession.sendMessage(id,on);
            return;
        }
        funOnHook.add(uid,pid);
        on.put(enumCMKey.status.name(), true);
        funSession.sendMessage(id,on);
    }
    public void removeOnHook(String id,JsonNode message){
        ObjectMapper om=new ObjectMapper();
        ObjectNode on=om.createObjectNode();
        on.put(enumCMKey.mark.name(), enumCMMark.removeOnHook.ordinal());
        on.put(enumCMKey.timestamp.name(), System.currentTimeMillis());
        on.put(enumCMKey.status.name(), false);
        if(!funSession.isOnlineOfId(id,on))return;
        if(!funUserTest.hasPID(id,message,on))return;
        String uid=funSession.getUIdOfId(id);
        String pid=message.get(enumCMKey.pid.name()).asText();
        if(!funOnHook.isExit(uid,pid)){
            on.put(enumCMKey.tip.name(), enumCMTip.not_pet_on_hook.getTip());
            funSession.sendMessage(id,on);
            return;
        }
        funOnHook.remove(uid,pid);
        on.put(enumCMKey.status.name(), true);
        funSession.sendMessage(id,on);
    }
}
